package org.example;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DaoStudent {
    public int update(Student s) {
        Connection conn = JDBCConn.getConnection();
        PreparedStatement stmt = null;
        String sql = "update s_student set * values(?,?,?,?,?,?,?,?,?,?,?,?,?) where stuno=?";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setLong(1, s.getStuno());
            stmt.setLong(14, s.getStuno());
            stmt.setString(2, s.getSname());
            stmt.setString(3, s.getSex());
            stmt.setLong(4, s.getTelephone());
            stmt.setLong(5, s.getQq());
            stmt.setLong(6, s.getWechat());
            stmt.setLong(7, s.getGradeid());
            stmt.setString(8, s.getEnterdate());
            stmt.setString(9, s.getFromcity());
            stmt.setString(10, s.getBirthday());
            stmt.setString(11, s.getAddress());
            stmt.setLong(12, s.getPass());
            stmt.setLong(13, s.getIdcardpic());
            int i = stmt.executeUpdate(sql);
            return i;
        } catch (SQLException e) {
            JDBCConn.close(null, stmt, conn);
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public int del(Long stuno) {
        Connection conn = JDBCConn.getConnection();
        PreparedStatement stmt = null;
        String sql = "delete s_student where stuno=?";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setLong(1, stuno);
            int i = stmt.executeUpdate(sql);
            return i;
        } catch (SQLException e) {
            JDBCConn.close(null, stmt, conn);
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


    public Student get(Long stuno) {
        Connection conn = JDBCConn.getConnection();
        PreparedStatement stmt = null;
        String sql = "select * from s_student where stuno=?";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setLong(1, stuno);
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                return new Student(rs.getLong("stuno"), rs.getString("sname"),
                        rs.getString("sex"), rs.getLong("telephone"),
                        rs.getLong("qq"),rs.getLong("wechat"), rs.getLong("gradeid"),
                        rs.getString("enterdate"), rs.getString("fromcity"),
                        rs.getString("birthday"),rs.getString("address"),
                        rs.getLong("pass"),rs.getLong("idcardpic"));
            }
        } catch (SQLException e) {
            JDBCConn.close(null, stmt, conn);
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return null;
    }

    public List<Student> search(int pageNo, int pageSize) {
        Connection conn = JDBCConn.getConnection();
        PreparedStatement stmt = null;
        List<Student> students = new ArrayList<>(pageSize);
        String sql = "select * from s_grade order by id limit ?,?";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setLong(1, pageNo);
            stmt.setLong(2, pageSize);
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                students.add(new Student(rs.getLong("stuno"), rs.getString("sname"),
                        rs.getString("sex"), rs.getLong("telephone"),rs.getLong("qq"),
                        rs.getLong("wechat"), rs.getLong("gradeid"),
                        rs.getString("enterdate"),rs.getString("fromcity"),
                        rs.getString("birthday"),rs.getString("address"),
                        rs.getLong("pass"),rs.getLong("idcardpic")));
            }
            return students;
        } catch (SQLException e) {
            JDBCConn.close(null, stmt, conn);
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
